<template>
  <div class="page-container" style="width:99%;margin-top:15px;">
    <el-row :gutter="20">
      <!-- 系统公告 -->
      <el-col :xs="24" :sm="24" :md="12" :lg="12">
        <el-card class="update-log">
          <div class="clearfix">
            <span>系统公告</span>
          </div>
          <el-collapse accordion>
            <el-collapse-item v-for="(item,index) in noticeArray" :key="index" :title="'标题：'+item.title+' 创建时间：'+item.createTime">
              <div v-html="item.content"></div>
            </el-collapse-item>
          </el-collapse>
        </el-card>
      </el-col>
      
      <!-- 学生五边形战士图 -->
      <el-col :xs="24" :sm="24" :md="12" :lg="12" v-if="isStudent">
        <el-card class="warrior-chart">
          <div class="clearfix">
            <span>个人成绩分析</span>
          </div>
          <student-radar-chart :student-num="studentNum" :term-id="currentTermId"></student-radar-chart>
        </el-card>
      </el-col>
    </el-row>
    
    <el-dialog title="修改密码" width="40%" :visible="updatePwdDialogVisible" >
      <el-form :model="updatePwdDataForm" label-width="100px" :rules="updatePwdDataFormRules" ref="updatePwdDataForm" :size="size">
        <el-form-item label="原密码" prop="password">
          <el-input v-model="updatePwdDataForm.password" type="password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="updatePwdDataForm.newPassword" type="password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="comfirmPassword">
          <el-input v-model="updatePwdDataForm.comfirmPassword" type="password" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size" type="primary" @click.native="updatePassword" :loading="updatePwdLoading">{{$t('action.comfirm')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import StudentRadarChart from '@/components/StudentRadarChart'

export default {
  components: {
    StudentRadarChart
  },
  data() {
    return {
      onlineUser: 0,
      accessTimes: 0,
      size: 'small',
      updatePwdDialogVisible: false,
      updatePwdLoading: false,
      updatePwdDataForm: {
        password: '',
        newPassword: '',
        comfirmPassword: ''
      },
      updatePwdDataFormRules: {
        password: [
          { required: true, message: '请输入原密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        comfirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' }
        ]
      },
      noticeArray: [],
      studentNum: '',
      currentTermId: null,
      isStudent: false
    }
  },
  computed: {
    mainTabs: {
      get () { return this.$store.state.tab.mainTabs },
      set (val) { this.$store.commit('updateMainTabs', val) }
    }
  },
  methods: {
    isOpenDialog() {
      this.$api.user.findByName({name:sessionStorage.getItem('user')}).then(res => {
        if(res.code === 200) {
          this.updatePwdDialogVisible = res.data.status == 2;
          this.isStudent = res.data.type === 'student';
          if(this.isStudent) {
            this.studentNum = res.data.name;
            this.getCurrentTerm();
          }
        }
      });
    },
    
    getNotices() {
      this.$api.sysNotice.findList().then(res => {
        if(res.code == 200) {
          this.noticeArray = res.data
        }
      })
    },
    
    getCurrentTerm() {
      // 获取当前学期
      this.$api.busTerm.findCurrentTerm().then(res => {
        if(res.code === 200 && res.data) {
          this.currentTermId = res.data.id;
        } else {
          // 如果没有当前学期，使用findOptions获取可用学期
          this.$api.busTerm.findOptions(1).then(optRes => {
            if(optRes.code === 200 && optRes.data && optRes.data.length > 0) {
              this.currentTermId = optRes.data[0].id;
            }
          });
        }
      });
    },

    // 修改密码
    updatePassword: function () {
      this.$refs.updatePwdDataForm.validate((valid) => {
        if (valid) {
          if(this.updatePwdDataForm.newPassword == this.updatePwdDataForm.password) {
            this.$message({message: '新密码与原密码一致', type: 'error'})
            return
          }
          if(this.updatePwdDataForm.newPassword != this.updatePwdDataForm.comfirmPassword) {
            this.$message({message: '新密码与确认新密码不一致', type: 'error'})
            return
          }
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.updatePwdLoading = true
            let params = {password:this.updatePwdDataForm.password, newPassword:this.updatePwdDataForm.newPassword}
            this.$api.user.updatePassword(params).then((res) => {
              this.updatePwdLoading = false
              if(res.code == 200) {
                this.$message({ message: '操作成功', type: 'success' })
                this.$refs['updatePwdDataForm'].resetFields()
                this.deleteCookie('token')// 清空Cookie里的token
                this.logoutApi()
                $router.push('/')
              } else {
                this.$message({message: '操作失败, ' + res.msg, type: 'error'})
              }
              this.updatePwdDialogVisible = false
            })
          })
        }
      })
    },
    
    // 清除缓存并退出登录
    // 清除Cookie
    deleteCookie(name) {
      var myDate = new Date()
      myDate.setTime(-1000) // 设置过期时间
      document.cookie = name+"=''; expires="+myDate.toGMTString();
    },
    
    logoutApi() {
      this.mainTabs = []
      sessionStorage.removeItem("user")
      this.$router.push("/login")
      this.$api.login.logout().then((res) => {
      }).catch(function(res) {
      })
    }
  },
  mounted() {
    this.isOpenDialog()
    this.getNotices()
  }
}
</script>

<style>
.page-container {
  padding: 0 20px;
}

.el-card {
  margin-bottom: 20px;
}

.update-log, .warrior-chart {
  height: 100%;
}

.update-log .clearfix, .warrior-chart .clearfix {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 15px;
  color: #303133;
}

.carousel {
  padding-left: 20px;
  padding-right: 20px;
  margin-right: 20px;
}
.carousel h2 {
  color: #475669;
  font-size: 22px;
  opacity: 1.75;
  line-height: 100px;
  margin: 0;
}
.carousel ul {
  color: #475669;
  font-size: 15px;
  opacity: 1.75;
  line-height: 40px;
  margin: 0;
}
.carousel-item-intro h2 {
  color: #ffffff;
  font-size: 22px;
  opacity: 1.75;
  line-height: 80px;
  margin: 0;
}
.carousel-item-intro ul {
  color: #ffffff;
  font-size: 15px;
  opacity: 1.75;
  line-height: 65px;
  padding: 5px;
  margin: 0;
}
.carousel-item-func h2 {
  color: #3f393b;
  font-size: 22px;
  opacity: 1.75;
  line-height: 50px;
  margin: 0;
}
.carousel-item-func ul {
  color: #3f393b;
  font-size: 15px;
  opacity: 1.75;
  line-height: 30px;
  text-align: left;
  padding-left: 90px;
  margin: 0;
}
.carousel-item-env h2 {
  color: #475669;
  font-size: 22px;
  opacity: 1.75;
  line-height: 50px;
  margin: 0;
}
.carousel-item-env ul {
  color: #475669;
  font-size: 15px;
  opacity: 1.75;
  line-height: 35px;
  text-align: left;
  padding-left: 110px;
  margin: 0;
}
.carousel-item-intro {
  background-color: #19aaaf73;
  -webkit-border-radius: 25px;
  border-radius: 25px;
  -moz-border-radius: 15px;
  background-clip: padding-box;
  box-shadow: 0 0 25px #a3b3b965;
}
.carousel-item-func {
  background-color: #19aaaf73;
  -webkit-border-radius: 25px;
  border-radius: 25px;
  -moz-border-radius: 15px;
  background-clip: padding-box;
  box-shadow: 0 0 25px #a3b3b965;
}
.carousel-item-env {
  background-color: #19aaaf73;
  -webkit-border-radius: 25px;
  border-radius: 25px;
  -moz-border-radius: 15px;
  background-clip: padding-box;
  box-shadow: 0 0 25px #a3b3b965;
}
.carousel-item-intro {
  background-color: #b95e5e;
}
.carousel-item-func {
  background-color: #52c578;
}
.carousel-item-env {
  background-color: #41a7b9;
}
</style>
